package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author ：HX
 * @date ：Created in 2019/12/13
 * @description ：角色控制器
 * @version: 1.0
 */
@RestController
@RequestMapping("role")
public class RoleController {
    @Reference
    private RoleService roleService;

    //    分页查询
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleService.findPage(queryPageBean);
    }
    //    新增
    @RequestMapping("add")
    public Result add(Integer[] permissionIds, Integer[] menuIds, @RequestBody Role role){
        try{
            roleService.add(permissionIds,menuIds,role);
        }catch (RuntimeException re){
            re.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
    }
    //    编辑回显
    @RequestMapping("findById")
    public Result findById(Integer id){
        Role role = roleService.findById(id);
        Map map=new HashMap();
        map.put("role",role);

        if (role!=null){
            Set<Permission> permissions = role.getPermissions();
            if (permissions!=null&&permissions.size()>0){
                List<Integer> permissionIds=new ArrayList();
                for (Permission permission : permissions) {
                    permissionIds.add(permission.getId());
                }
                map.put("permissionIds",permissionIds);
            }

            LinkedHashSet<Menu> menus = role.getMenus();
            if (menus!=null&&menus.size()>0){
                List<Integer> menuIds=new ArrayList<>();
                for (Menu menu : menus) {
                    menuIds.add(menu.getId());
                }
                map.put("menuIds",menuIds);
            }
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,map);
        }
        return new Result(true, MessageConstant.QUERY_ROLE_FAIL);
    }
    //    编辑
    @RequestMapping("edit")
    public Result edit(Integer[] permissionIds, Integer[] menuIds, @RequestBody Role role){
        try{
            roleService.edit(permissionIds,menuIds,role);
        }catch (RuntimeException re){
            re.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
    }
//    删除
    @RequestMapping("deleteById")
    public Result deleteById(Integer id){
        try{
            roleService.deleteById(id);
        }catch (RuntimeException re){
            re.printStackTrace();
            return new Result(false, re.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
    }
}
